local _ENV,_START = hf_setenv('shapedata',true)

local skynet = require "skynet"
local sharedata = require "skynet.sharedata"
local util = require "util"
local shape_common = require "shape_common"

local math_random = math.random
local math_floor  = math.floor
local table_insert = table.insert

function init()
    global_configs = nil
   -- last_update_time = nil
	curr_shape_num = 0
	replace_shape = nil
    hf_set_sentinel_end(_ENV)
end

local function init_all_shape(lines_num)
	local all_shape = {}
	for i = 1, lines_num do
		if not all_shape[i] then
			all_shape[i] = {}
		end
		for k = 1,shape_common.BIG_WIN do        --内存中存放大亏，小亏，小赢，大赢类型
		--for k = 1,shape_common.JACKPOT do
			all_shape[i][k] = {}
		end
	end

	return all_shape
end

local function print_all_shape(all_shape)
	local line_shape_num = global_configs.fruit_rand_shape.line_shape_num
	local tab = {}
	for k, v in pairs(all_shape) do
		for k1,v1 in pairs(v) do
			--print("===========line class=====",k,k1,#v1,line_shape_num[k][k1][1],line_shape_num[k][k1][2])
			--print("===========line class=====",k,k1,#v1)
		end
	end
end

local function class_shape(lines,one_shape,max_num,all_shape)    --将图分类
	local line_shape_num = global_configs.fruit_rand_shape.line_shape_num
	for i = 1, max_num do
		local times,free_times,total_caijin_rate = shape_common.get_total_times(i,lines)
		--免费次数和奖金不能同时获得
		if free_times > 0 and total_caijin_rate > 0 then
			goto continue
		end
		local type = shape_common.get_one_shape_type(lines,i)
		if all_shape[i][type] and #all_shape[i][type] < line_shape_num[i][type][2] then
		--if all_shape[i][type] then
			table_insert(all_shape[i][type],one_shape)
		end
		::continue::
	end
end

local function is_enough(all_shape)
	local line_shape_num = global_configs.fruit_rand_shape.line_shape_num
	if not line_shape_num then return end

	for line, shape_list in pairs(all_shape) do
		for kind, kind_shape in pairs(shape_list) do
			if #kind_shape < line_shape_num[line][kind][1] then	   --比要求生成的最小值要小，还要继续生成
				return false
			end
		end
	end
	
	return true
end

local function produce_rand_data()
	--print("produce_rand_data================",global_configs.fruit_rand_shape)
	local produce_rate = global_configs.fruit_rand_shape.shape_rate   --生成每张图的概率
	local line_rate = global_configs.fruit_rand_shape.line_rate  --图案倍数
	local award_lines = global_configs.fruit_rand_shape.award_lines --可以获奖的连线
	local caijin_rate = global_configs.tiger_rate.caijin_rate

	--print("enter===========111",os.time(),tostring_r(line_rate),tostring_r(caijin_rate),tostring_r(award_lines))
	local all_shape = init_all_shape(#award_lines)
	local total_award_num  = 0
	local total_weight_map = shape_common.get_total_weight(produce_rate)

	--print("===============total_weight_map[line][row]=====",tostring_r(total_weight_map))

	local total_num = 0
	while not is_enough(all_shape) and total_num < shape_common.MAX_SHAPE_NUM do
	--while total_num < shape_common.MAX_SHAPE_NUM do
		local one_shape = shape_common.init_one_shape(total_weight_map,produce_rate)	
		local lines = shape_common.check_line(one_shape,line_rate,award_lines,caijin_rate)
		class_shape(lines,one_shape,#award_lines,all_shape)     --将生成的图片分类
		total_num = total_num + 1
	end

	print("enter===========222",os.time(),total_num,#all_shape,total_award_num)
	print_all_shape(all_shape)

	return all_shape
end

function do_produce_data()
	local award_lines = global_configs.fruit_rand_shape.award_lines --可以获奖的连线
	local produce_rate = global_configs.fruit_rand_shape.shape_rate   --生成每张图的概率
	local line_rate = global_configs.fruit_rand_shape.line_rate  --图案倍数
	local caijin_rate = global_configs.tiger_rate.caijin_rate
	local total_num = 0   --每30s次生成100张

	local total_weight_map = shape_common.get_total_weight(produce_rate)

	if not replace_shape then
		replace_shape = init_all_shape(#award_lines)
	end

	if is_enough(replace_shape) then    --数量够了，可以替换内存中原有图片了
		print("==========now replace===",curr_shape_num)
		curr_shape_num = 0
		local data = replace_shape
		sharedata.update("shapedata", data)
		replace_shape = nil

		return
	end

	while not is_enough(replace_shape) and total_num < 100 do
		local one_shape = shape_common.init_one_shape(total_weight_map,produce_rate)	
		local lines = shape_common.check_line(one_shape,line_rate,award_lines,caijin_rate)
		class_shape(lines,one_shape,#award_lines,replace_shape)     --将生成的图片分类
		total_num = total_num + 1
		curr_shape_num = curr_shape_num + 1
	end
	
	print_all_shape(replace_shape)
--	print("=======curr_shape_num====",curr_shape_num)
end

local function do_produce_data_routine()
	while true do
		local ok, ret = xpcall(do_produce_data, debug.traceback)
		if not ok then
			errlog(ret)
		end

		skynet.sleep(100 * 30)
	end
end

local CMD = {}

function CMD.start()
	local data = produce_rand_data()      --服务启动的时候先生成一份
	sharedata.update("shapedata", data)

	skynet.fork(do_produce_data_routine)
    return skynet.retpack(true)
end

function dispatch_cmd(session,source,action,...)
    CMD[action](...)
end

function start()
    skynet.start(function()
	    skynet.dispatch("lua", function(session,source,action,...)
	        dispatch_cmd(session,source,action,...)
	    end)

	    global_configs = setmetatable({},{
            __index = function(t,k) 
                --return sharedata.query("global_configs")[k]
                return sharedata.query(k)
            end
        })
	    --global_configs = sharedata.query("global_configs")
	    _G.global_configs = global_configs
	end)
end

_START(function()
    print('now this service is starting')
    init()
    start()
end)
